﻿using ImTools;
using Prism.Commands;
using Prism.Ioc;
using Prism.Mvvm;
using Prism.Regions;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WPF.Project.Demo.Common.Models;

namespace WPF.Project.Demo.ViewModels
{
    /// <summary>
    /// 待办事项
    /// </summary>
    public class ToDoViewModel : NavigationViewModel
    {
        public ToDoViewModel(IContainerProvider provider) : base(provider)
        {
            ToDoDtos = new ObservableCollection<ToDoDto>();
            SelectedCommand = new DelegateCommand<ToDoDto>(Selected);
            ExecuteCommand = new DelegateCommand<string>(Execute);
        }

        private void Execute(string obj)
        {
            switch (obj)
            {
                case "ADD":
                    Add();
                    break;
                case "SAVE":
                    Save();
                    break;
                case "QUERY":
                    Query();
                    break;
                default:
                    break;
            }
        }

        private bool isRightDrawerOpen;
        public bool IsRightDrawerOpen
        {
            get { return isRightDrawerOpen; }
            set { isRightDrawerOpen = value; RaisePropertyChanged(); }
        }


        /// <summary>
        /// 查询条件
        /// </summary>
        private string search;
        public string Search
        {
            get { return search; }
            set { search = value; RaisePropertyChanged(); }
        }


        /// <summary>
        /// 编辑选中时 对象
        /// </summary>
        private ToDoDto currentDto;
        public ToDoDto CurrentDto
        {
            get { return currentDto; }
            set { currentDto = value; RaisePropertyChanged(); }
        }

        /// <summary>
        /// 查询方法
        /// </summary>
        private void Query()
        {
            //ToDoDtos = new ObservableCollection<ToDoDto>();
            ToDoDtos.Add(new ToDoDto { Id = 0, Title = "测试查询", Content = "测试查询" });

        }

        /// <summary>
        /// 保存方法
        /// </summary>
        /// <exception cref="NotImplementedException"></exception>
        private void Save()
        {
            throw new NotImplementedException();
        }


        private void Add()
        {
            IsRightDrawerOpen = true;
        }

        private void Selected(ToDoDto obj)
        {
            //打开窗口
            IsRightDrawerOpen = true;

            //接口服务 查询数据
            // 数据赋给 CurrentDto
            ToDoDto toDoDto = new ToDoDto
            {
                Id = 1,
                Title = "测试编辑功能",
                Content = "测试编辑功能",
                Status = 1
            };

            CurrentDto = toDoDto;

        }

        public DelegateCommand<ToDoDto> SelectedCommand { get; private set; }
        public DelegateCommand<string> ExecuteCommand { get; private set; }


        ObservableCollection<ToDoDto> toDoDtos;
        public ObservableCollection<ToDoDto> ToDoDtos
        {
            get { return toDoDtos; }
            set { toDoDtos = value; RaisePropertyChanged(); }
        }

        void CreateToDoList()
        {
            //接口服务 真实查询
            for (int i = 0; i < 10; i++)
            {
                ToDoDtos.Add(new ToDoDto { Id = i, Title = "标题" + i, Content = "测试数据", Status = 0 });
            }
            for (int i = 0; i < 10; i++)
            {
                ToDoDtos.Add(new ToDoDto { Id = i, Title = "标题Pro" + i, Content = "测试数据Pro", Status = 1 });
            }
        }

        /// <summary>
        /// 重写 OnNavigatedTo  
        /// </summary>
        /// <param name="navigationContext"></param>
        public override async void OnNavigatedTo(NavigationContext navigationContext)
        {
            base.OnNavigatedTo(navigationContext);

            UpdateLoading(true);
            await Task.Delay(2000);
            CreateToDoList();
            UpdateLoading(false);
        }



    }
}
